<?php
include APPLICATION_PATH."/models/base/UserBase.php";
class Model_User extends Model_UserBase
{
}
class Model_UserMapper extends Model_UserMapperBase
{
     public function authenUser($username,$password,$remenber=false){
        $password=trim($password);
        $userInfo = GlobalLib::query('select * from user where status=1 and username = ? and password = ? ', array($username, ($password)));
        if(count($userInfo) == 1 && !empty($userInfo[0]))
        {
            require_once('Zend/Session/Namespace.php');
            $session = new Zend_Session_Namespace('Zend_Auth_User_Info');
            $session->userInfo = $userInfo[0];
            return true;
        }
        //GlobalLib::toS($userInfo);
        return false;
    }
    public static function getlastid(){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select= 'select max(id) as id from user';
        $stmt=$db->query($select);
        $row = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        return $row[0]->id;        
    }
    //
     public function fetchAllL($id_kd){
        $db = Zend_Db_Table::getDefaultAdapter();      
       $select="select t1.id,t1.username,t1.password,t1.phone,t1.first_name,t1.last_name,t1.created,t1.created_by,t1.modified,t1.birthday,t1.modified_by,t1.order,t1.status from user as t1 left join agent as t2 on t1.id=t2.user_id left join admin_area as t3 on t2.area_id = t3.area_id left join admin as t4 on t3.admin_id = t4.id where t4.id ='".$id_kd."' group by t1.id";        
        $stmt=$db->query($select);
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();       
        foreach ($rows as $row) {
            $entry = new Model_User();   
            $entry->setId($row->id)
                   ->setId($row->id)
                    ->setUserName($row->username)
                    ->setPassword($row->password)
                    ->setFirst_Name($row->first_name)
                    ->setLast_Name($row->last_name)
                    ->setPhone($row->phone)
                    ->setCreated($row->created)
                    ->setCreated_By($row->created_by)
                    ->setModified($row->modified)
                    ->setModified_By($row->modified_by)
                    ->setBirthday($row->birthday)
                    ->setOrder($row->order)
                    ->setStatus($row->status);
            $entries[]=$entry;
        }
        return $entries;
    }
    
    public static function findbyusername($username){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select= "select max(id) as id from user where username='$username'";
        $stmt=$db->query($select);
        $row = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        if($row==NULL){
            return 1;
        } else {
            return $row[0]->id;
        }
    }
}
class Model_KiemTra {
    public function KiemTra($username,$password){
        $password=trim($password);
        $userInfo = GlobalLib::query('select * from user where status=1 and username = ? and password = ? ', array($username, ($password)));
        $adminInfo= GlobalLib::query('select * from admin where status=1 and user_name = ? and password = ? ', array($username, ($password)));
        if(!empty($userInfo[0])){
            return 1;
        } 
        if(!empty($adminInfo[0])){
            if($adminInfo[0]['admin_group_id']==2){
                return 3;
            } else {
                return 2;
            }
        }
    }
    
}